package com.github.codemperor.core.aws.s3;

import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class S3Utils {
    private AwsCredentials awsCreds;
    private Region region;

    public S3Utils(AwsCredentials awsCreds, Region region) {
        this.awsCreds = awsCreds;
        this.region = region;
    }

    private S3Client createS3Client() {
        return S3Client.builder().region(region)
                .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
                .build();
    }

    /**
     * 简单上传
     *
     * @param file       controlller传来的文件
     * @param bucketName 桶名称
     * @param objectName 对象名称
     * @param publicRead 是否公共读
     * @return
     * @throws IOException
     */
    public PutObjectResponse upload(MultipartFile file, String bucketName, String objectName, boolean publicRead) throws IOException {
        S3Client s3 = createS3Client();
        ObjectCannedACL acl = ObjectCannedACL.PRIVATE;
        if (publicRead) {
            acl = ObjectCannedACL.PUBLIC_READ;
        }
        //不支持svg上传展示，会自动被变成html，需要加上.contentType("image/svg+xml")
        PutObjectRequest req = PutObjectRequest.builder().bucket(bucketName).key(objectName).acl(acl).build();
        return s3.putObject(
                req,
                RequestBody.fromInputStream(file.getInputStream(), file.getSize())
        );
    }

    /**
     * 简单上传
     *
     * @param file       文件
     * @param bucketName 桶名称
     * @param objectName 对象名称
     * @return
     */
    public PutObjectResponse upload(File file, String bucketName, String objectName) {
        S3Client s3 = createS3Client();
        //不支持svg上传展示，会自动被变成html，需要加上.contentType("image/svg+xml")
        PutObjectRequest req = PutObjectRequest.builder().bucket(bucketName).key(objectName).acl(ObjectCannedACL.PUBLIC_READ).build();
        return s3.putObject(
                req,
                RequestBody.fromFile(file)
        );
    }

    /**
     * 获取文件
     *
     * @param bucketName
     * @param objectName
     * @return
     */
    public void getObjects(String bucketName, String objectName, String ec2Path) {
        S3Client s3 = createS3Client();
        s3.getObject(GetObjectRequest.builder().bucket(bucketName).key(objectName).build(),
                ResponseTransformer.toFile(Paths.get(ec2Path)));

    }

    /**
     * 删除文件
     *
     * @param bucketName
     * @param objectName
     */
    public void deleteObject(String bucketName, String objectName) {
        S3Client s3 = createS3Client();
        s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectName).build());
    }
}
